/*
* VFS_Group 19 - IIIT-B, MTech Program, 1st Semester, file_name Management.
*
* Implementation of HASHTABLE and all operations on HASHTABLE.
*
*/

#ifndef STANDARD_IO
#define STANDARD_IO

#include<stdio.h>

#endif

#ifndef STANDARD_LIB
#define STANDARD_LIB

#include<stdlib.h>
#include<string.h>

#endif


#include "../include/hashtable.h"
#include "../include/global_header.h"

// 1. Compute slot in the Hashtable

// 2. search based on file name

// 3. Insert to hashtable

// 4. Display

// 5. Delete from hashtable based on the path

hash_ptr initialize_hashtable(){
    int i;

    hash_ptr*  temp_hash_root = (hash_ptr*)malloc(sizeof(hash_ptr)*TABSIZ);

    for(i=0;i<52;i++){
        temp_hash_root[i] = (hash_ptr)malloc(sizeof(hash_node));
    }

    htable = temp_hash_root;
    return temp_hash_root;
}

unsigned int compute_hashslot(char name[]) {
    int c = *name;
        if((c>=65) & (c<=90))
     return ((c%65)+26);
    if ((c>=97) & (c<=122))
      return (c%97);
    else return 0;
}

char *hash_search(char name[]) {

    hashtable *item;
    char *allSearch,*searname;
    int len;
    int flag=0;
    allSearch=(char*)malloc(sizeof(char)*1024);

    item=htable[compute_hashslot(name)];

    len=strlen(name);

    if((item = htable[compute_hashslot(name)]) != NULL)
    {       // printf("%s",item->filepath);
            for (item = htable[compute_hashslot(name)]; item; item = item->next){
            searname=(char*)malloc(sizeof(char)*1024);
            strncpy(searname,item->filename,len);
            if (strcmp(name,searname) == 0){
                    if(flag==0){
                       strcpy(allSearch,item->filepath);
                       flag=1;
                    }
                    else
                       strcat(allSearch,item->filepath);
                  strcat(allSearch," ");
              }
            }
    }

    return allSearch;
}

void display_hashtable() {

    int i=0;
    hashtable *temp;

    for(i=0;i< 52;i++) {

       temp=htable[i];
       while(temp!=NULL) {
         // printf("\n%d -  %s  %s\n", i, temp->filename,temp->filepath);
         temp=temp->next;
       }
    }
    // printf("Coming out of display hashtable");
}

hashtable *insert_hash(char full_path[],char file_name[])
{
    hashtable *item;
    int h;

    if ((item = malloc(sizeof (*item))) != NULL) {
       strcpy(item->filename,file_name);
       strcpy(item->filepath,full_path);
       h = compute_hashslot(file_name);

       item->next = htable[h];
       htable[h] = item;
       //display_hashtable();
    }
    else{
      return NULL;
    }

    return item;
}

int hash_delete(char *full_path)
{
    hashtable *temp,*prev;

    char *file_name;
    file_name=strrchr(full_path,'/') + 1;

    int h = compute_hashslot(file_name);
    temp=htable[h];

    //// printf("%s full path :",full_path);
    while(temp!=NULL)
    {
        //// printf("temp path %s",temp->filepath);
        if(!strcmp(temp->filepath,full_path))
        {
            if(temp==htable[h])
            {
                    htable[h]=temp->next;
                    free(temp);
                    return 1;
            }
            else
            {
                    prev->next=temp->next;
                    free(temp);
                    return 1;
            }
        }
        else
        {
            prev=temp;
            temp= temp->next;
        }
    }

    return 0;
}
